﻿using LibBinary.DataSource;
using LibBinary.FileData;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LibBinary.FileDeclaration.Text
{
    public class RelativeReferenceDeclaration : IFileDeclaration
    {
        private readonly int[] index;

        public RelativeReferenceDeclaration(params int[] index)
        {
            this.index = index;
        }

        public AbstractFileData Read(TextFileInputSource src, AbstractFileData parentNode)
        {
            AbstractFileData node = parentNode;
            foreach (var i in index)
            {
                if (node == null)
                {
                    throw new ReadingFormatException("Invalid path in RelativeReference.");
                }
                if (i == -1)
                {
                    node = node.Parent;
                }
                else
                {
                    node = node.GetChild(i);
                }
            }
            if (node == null)
            {
                throw new ReadingFormatException("Invalid path in RelativeReference.");
            }
            return node;
        }

        public void Write(TextFileOutputSource src, AbstractFileData data)
        {
        }
    }
}
